package work1.jdbcbooksystem;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class Test {
    static Scanner inInt = new Scanner(System.in);
    static Scanner inString = new Scanner(System.in);
    static Scanner intDouble = new Scanner(System.in);
    static BookDaoDB dao = new BookDaoDB();

    public static void main(String[] args) {

        while (true) {
            System.out.println("-------图书管理系统-------");
            System.out.println("1------添加书籍");
            System.out.println("2------查询书籍");
            System.out.println("3------删除书籍");
            System.out.println("4------更新书籍");
            System.out.println("5------退出系统");
            int choose = inInt.nextInt();
            switch (choose) {
                case 1:
                    insertUI();
                    break;
                case 2:
                    select();
                    break;
                case 3:
                    deleteUI();
                    break;
                case 4:
                    updateUI();
                    break;
                case 5:
                    exitUI();
                    break;
                default:
                    System.out.println("输入有误，请输入1-5：");
                    break;
            }
        }
    }

    /*退出*/
    private static void exitUI() {
        dao.exit();
    }

    /*更新书籍*/
    private static void updateUI() {
        System.out.println("----更新界面----");
        System.out.println("请输入药修改图书的名称：");
        String name = inString.nextLine();
        Book book = dao.query(name);
        if (book == null) {
            System.out.println("修改图书不存在");
        } else {
            System.out.println("请输入要修改的图书名称");
            String n = inString.nextLine();
            System.out.println("请输入要修改的出版社：");
            String press = inString.nextLine();
            System.out.println("请输入要修改的作者：");
            String author = inString.nextLine();
            System.out.println("请输入要修改的价格：");
            double price = intDouble.nextDouble();
            Book b = new Book(book.getId(), n, press, author, price);
            int c = dao.update(b);
            if (c > 0) {
                System.out.println("更新成功");
            } else {
                System.out.println("更新失败");
            }
        }
    }

    /*删除书籍*/
    private static void deleteUI() {
        System.out.println("-----删除界面-----");
        System.out.println("请输入要删除的图书名称");
        String name = inString.nextLine();
        Book book = dao.query(name);
        if (book==null){
            System.out.println("修改图书不存在");
        }else{

            int c = dao.delete(name);
            if (c > 0) {
                System.out.println("更新成功");
            } else {
                System.out.println("更新失败");
            }
        }
    }

    /*查询书籍*/
    private static void select() {
        System.out.println("-----所有图书信息如下：-------");
        System.out.println("编号\t出版社\t作者\t价格");

        ArrayList<Book> arrayList = dao.selectAll();

        Iterator<Book> iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            Book book = iterator.next();
            System.out.println(book.getId() + "\t" + book.getName()
                    + "\t" + book.getPress() + "\t" + book.getAuthor() + "\t"
                    + book.getPrice());
        }

    }

    /*添加书籍*/
    private static void insertUI() {
        System.out.println("-----添加界面-----");
        System.out.println("请输入图书名称");
        String name = inString.nextLine();
        System.out.println("请输入出版社");
        String press = inString.nextLine();
        System.out.println("请输入作者");
        String author = inString.nextLine();
        System.out.println("请输入图书价格");
        double price = intDouble.nextDouble();

        Book book = new Book(name, press, author, price);
        int c = dao.insert(book);

        if (c > 0) {
            System.out.println("添加成功");
        } else {
            System.out.println("图书名称重复，添加失败");
        }
    }
}
